#!/usr/bin/env python3
"""
调试题目数据的脚本
"""

from app.core.database import SessionLocal
from app.models import Question, Grade, KnowledgePoint
from app.services.question_service import QuestionService
from app.schemas.test_config import TestConfig

def main():
    db = SessionLocal()
    try:
        # 检查所有题目
        all_questions = db.query(Question).all()
        print(f"题目总数: {len(all_questions)}")
        
        # 检查年级1的题目
        grade1_questions = db.query(Question).filter(Question.grade_id == 1).all()
        print(f"年级1的题目数: {len(grade1_questions)}")
        
        if grade1_questions:
            print("\n年级1的题目:")
            for q in grade1_questions[:3]:
                print(f"ID: {q.id}, 内容: {q.content[:50]}...")
        
        # 检查年级数据
        grades = db.query(Grade).all()
        print(f"\n年级总数: {len(grades)}")
        for g in grades:
            print(f"年级ID: {g.id}, 名称: {g.name}")
        
        # 尝试生成测试
        print("\n尝试生成测试...")
        config = TestConfig(
            grade_id=1,
            question_count=5,
            time_limit=30
        )
        
        service = QuestionService(db)
        try:
            test_paper = service.generate_test_paper(config)
            print(f"✅ 测试生成成功! ID: {test_paper.id}")
        except Exception as e:
            print(f"❌ 测试生成失败: {e}")
            import traceback
            traceback.print_exc()
            
    finally:
        db.close()

if __name__ == "__main__":
    main()